<?php

namespace Home\Controller;

class TaskController extends BaseController {

    private $model = null;

    function __construct() {
        parent::__construct();
        $this->menu = 2;
        $this->model = D('Task');
        $this->type = array(
            1 => '充值',
            2 => '提现',
            3 => '任务'
        );
        $this->status = array(
            0 => '<span style="color:#ec494e;">待处理</span>',
            1 => '<span style="color:#090;">已处理</span>'
        );
    }

    function index() {
        $this->second = 1;
        $this->display();
    }

    /**
     * 发布流量任务
     */
    function addTask() {
        if (IS_POST && IS_AJAX) {
            $data = array(
                'coding' => date('ymdHis') . rand(10, 99),
                'user_id' => parent::$user_id,
                'user_name' => parent::$user_name,
                'store_id' => I('post.store', 0, 'intval'),
                'package_id' => I('post.package', 0, 'intval'),
                'keywords' => I('post.keywords', '', 'trim'),
                'price' => I('post.price', '', 'trim'),
                'add_time' => time()
            );
            $data['store_name'] = $this->model->getStoreName($data['store_id']);
            $data['package_name'] = $this->model->getPackageName($data['package_id']);
            $result = $this->model->addTask($data);
            if ($result['status'] == 1) {
                $this->uploadImg($result['new_id']);
                $this->success($result['info']);
            } else {
                $this->error($result['info']);
            }
        } else {
            $store = $this->model->getStore(parent::$user_id);
            $this->assign('store', $store);
            $package = $this->model->getPackage();
            $this->assign('package', $package);
            $this->display();
        }
    }

    /**
     * 上传图片
     * @param type $id
     */
    private function uploadImg($id) {
        $upload = new \Think\Upload();
        $upload->maxSize = 2097152;
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath = './static/';
        $upload->savePath = 'img/';
        $upload->saveName = $id;
        $upload->saveExt = 'jpg';
        $upload->subName = '';
        $info = $upload->uploadOne($_FILES['img']);
        if ($info) {
            $img_path = '/static/' . $info['savepath'] . $info['savename'];
            $data = array('img' => $img_path);
            $this->model->modify($id, $data);
        } else {
            // 上传错误提示错误信息
            $this->error($upload->getError());
        }
    }

    function flowHall() {
        $key = I('get.key', '', 'trim');
        $where = array();
        if ($key != '') {
            $where['user_name|package_name|coding|title|keywords'] = array('LIKE', "%{$key}%");
        }
        $this->status = array(
            0 => '进行中',
            1 => '已完成'
        );
        $result = $this->model->getList($where);
        $this->assign('data', $result);
        $this->display();
    }

    function myTask() {
        $where = array('user_id' => parent::$user_id);
        $key = I('get.key', '', 'trim');
        if ($key != '') {
            $where['user_name|package_name|coding|title|keywords'] = array('LIKE', "%{$key}%");
        }
        $this->status = array(
            0 => '进行中',
            1 => '已完成'
        );
        $result = $this->model->getList($where);
        $this->assign('data', $result);
        $this->display();
    }

    function change() {
        $this->second = 2;
        $info = $this->model->getUserAmount(parent::$user_id);
        $this->assign('amount', $info);
        $this->display();
    }

    /**
     * 充值
     */
    function AccountTopupAlipay() {
        if (IS_POST) {
            $code = I('post.vcode', '', 'trim');
            if (!check_verify($code)) {
                $this->error('验证码错误！');
            }
            $amount = $this->model->getUserAmount(parent::$user_id);
            $data = array(
                'type' => 1,
                'user_id' => parent::$user_id,
                'user_name' => parent::$user_name,
                'order_no' => I('post.TransactionNo', '', 'trim'),
                'star_amount' => $amount,
                'alipay_account' => I('post.AliPay', '', 'trim'),
                'add_time' => time()
            );
            $result = $this->model->addChange($data);
            if ($result) {
                $this->success('提交成功！');
            } else {
                $this->error('提交失败！');
            }
        } else {
            $this->display();
        }
    }

    function MlipayAccount() {
        $amount = $this->model->getUserAmount(parent::$user_id);
        $this->assign('amount', $amount);
        $cash_amount = $this->model->getCashAmount(parent::$user_id);
        $sure_amount = $amount - $cash_amount;
        $this->assign('sure_amount', $sure_amount);
        $this->second = 3;
        $this->display();
    }

    function cash() {
        if (IS_POST) {
            $code = I('post.vcode', '', 'trim');
            if (!check_verify($code)) {
                $this->error('验证码错误！');
            }
            //账户余额
            $amount = $this->model->getUserAmount(parent::$user_id);
            $cash_amount = $this->model->getCashAmount(parent::$user_id);
            $sure_amount = $amount - $cash_amount;
            $data = array(
                'type' => 2,
                'user_id' => parent::$user_id,
                'user_name' => parent::$user_name,
                'alipay_account' => I('post.alipay_account', '', 'trim'),
                'alipay_name' => I('post.alipay_name', '', 'trim'),
                'transaction_amount' => I('post.amount', '', 'trim'),
                'star_amount' => $amount,
                'amount' => ($amount - I('post.amount', '', 'trim')),
                'add_time' => time()
            );
            if ($data['amount'] > $sure_amount) {
                $this->error('提交金额不可大于可用提现金额！');
            }
            $result = $this->model->addCash($data);
            if ($result) {
                $this->success('提交成功！');
            } else {
                $this->error('提交失败！');
            }
        } else {
            $this->display();
        }
    }

    /**
     * 交易记录
     */
    function TransactionRecords() {
        $this->second = 4;
        $this->display();
    }

    /**
     * 所有记录
     */
    function TransactionList() {
        $result = $this->model->getTransactionRecords(0, parent::$user_id);
        $this->assign('data', $result);
        $this->display();
    }

    /**
     * 充值记录
     */
    function changeList() {
        $result = $this->model->getTransactionRecords(1, parent::$user_id);
        $this->assign('data', $result);
        $this->display();
    }

    /**
     * 提现记录
     */
    function cashList() {
        $result = $this->model->getTransactionRecords(2, parent::$user_id);
        $this->assign('data', $result);
        $this->display();
    }

    /**
     * 任务记录
     */
    function taskList() {
        $result = $this->model->getTransactionRecords(3, parent::$user_id);
        $this->assign('data', $result);
        $this->display();
    }

    function details() {
        $id = I('get.id', 0, 'intval');
        $result = $this->model->getTaskInfo($id, parent::$user_id);
        $this->assign('info', $result);
        $package = $this->model->getPackageInfo($result['package_id']);
        $this->assign('package', $package);
        $this->display();
    }

}
